using System;
using System.Runtime.Serialization;
using EnvDTE;
using VSSDKAssist.Assets.Helpers;
using VSSDKAssist.Assets.References.Base;

namespace VSSDKAssist.Recipes.Common.References
{
	/// <summary>
	/// UnboundReference for a setup project
	/// </summary>
	[Serializable]
	public class SetupProjectReference : RecipeReferenceBase
	{
        #region Constants
        private const string IsVsPackageSetup = "IsVsPackageSetup";
        #endregion

		#region Constructors
		/// <summary>
        /// Initializes a new instance of the <see cref="SetupProjectReference"/> class.
		/// </summary>
		/// <param name="recipe">The recipe.</param>
        public SetupProjectReference(String recipe) : base(recipe) { }
		#endregion

		#region Public Implementation
		/// <summary>
		/// Determines whether the reference is enabled for a particular target item,
		/// based on the condition contained in the reference.
		/// </summary>
		/// <param name="target">The <see cref="T:System.Object"/> to check for references.</param>
		/// <returns>
		/// 	<see langword="true"/> if the reference is enabled for the given <paramref name="target"/>.
		/// Otherwise, <see langword="false"/>.
		/// </returns>
		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
		public override bool IsEnabledFor(object target)
		{
			Project project = target as Project;

			if(project == null)
			{
				return false;
			}

            if(project.Globals.get_VariableExists(IsVsPackageSetup))
            {
                string propertyValue = (string)project.Globals[IsVsPackageSetup];
                bool propertyValueBoolean;

                if(Boolean.TryParse(propertyValue, out propertyValueBoolean))
                {
                    if(propertyValueBoolean)
                    {
                        return true;
                    }
                }
            }

			return false;
		}

		/// <summary>
		/// See <see cref="P:Microsoft.Practices.RecipeFramework.IAssetReference.AppliesTo"/>.
		/// </summary>
		/// <value></value>
		public override String AppliesTo
		{
			get { return Properties.Resources.DslLanguageProjectReference; }
		}
		#endregion

		#region ISerializable Members

		/// <summary>
		/// Required constructor for deserialization.
		/// </summary>
		protected SetupProjectReference(SerializationInfo info, StreamingContext context)
			: base(info, context)
		{
		}

		#endregion ISerializable Members
	}
}
